/**
 * 防抖函数
 * @param {*} method 
 * @param {*} time 
 */
export function debounce (method, time) {
  var timer = null;
  return function () {
    var context = this;
    var ags = arguments;
    //在函数执行的时候先清除timer定时器;
    if (timer) clearTimeout(timer);
    timer = setTimeout(function () {
      method.apply(context, ags);
    }, time);
  }
}

/**
 * 判断类型
 * String
 * Number
 * Boolean
 * Function
 * Null
 * Undefined
 * Object
 * Array
 * Date
 * RegExp
 * @param {*} o 
 */
export const isType = (o) => {
  return Object.prototype.toString.call(o).slice(8, -1)
}


/**
 * 判断是否是基本数据类型
 * @param value 
 */
function isPrimitive (value) {
  return (typeof value === 'string' ||
    typeof value === 'number' ||
    typeof value === 'symbol' ||
    typeof value === 'boolean')
}

/**
 * 判断是否是一个js对象
 * @param value 
 */
function isObject (value) {
  return Object.prototype.toString.call(value) === "[object Object]"
}

/**
 * 深拷贝一个值
 * @param value 
 */
export function deepClone (obj, parent = null) {
  let result
  let _parent = parent
  while (_parent) {
    if (_parent.originalParent === obj) {
      return _parent.currentParent
    }
    _parent = _parent.parent
  }
  if (obj && typeof (obj) === 'object') {
    if (obj instanceof RegExp) {
      result = new RegExp(obj.source, obj.flags)
    } else if (obj instanceof Date) {
      result = new Date(obj.getTime())
    } else {
      if (obj instanceof Array) {
        result = []
      } else {
        let proto = Object.getPrototypeOf(obj)
        result = Object.create(proto)
      }
      for (let i in obj) {
        if (obj[i] && typeof (obj[i]) === 'object') {
          result[i] = deepClone(obj[i], {
            originalParent: obj,
            currentParent: result,
            parent: parent
          })
        } else {
          result[i] = obj[i]
        }
      }
    }
  } else {
    return obj
  }
  return result
}